import {
  request,
} from '../../utils/requestUtils';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    refreshTriggered: false,
    scrollTop:0,
    total:0,
    page: {
      pageNum: 1,
      pageSize: 20,
    },
    params: {
      keywords: ''
    },
    products: [],// 商品列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.onScrollRefresh()
    this.initType()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().setData({
      active: 0
    })
  },
  keywordsChange(e){
    this.setData({
      'params.keywords': e.detail
    })
  },
  searchClick(e){
    this.setData({
      products:[],
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  // 查询商品信息
  async getList() {
    const result = await request({
      url: `/bus/customer/product/list?pageNum=${this.data.page.pageNum}&pageSize=${this.data.page.pageSize}`,
      method: "POST",
      data: {
        params: this.data.params
      }
    })
    this.setData({
      refreshTriggered: false
    })
    if(result.data.code == 200){
      var products = this.data.products.concat(result.data.rows)
      this.setData({
        products: products,
        total: result.data.total
      })
    }
  },
  // 详情
  details(e){
    wx.navigateTo({
      url: '/pages/customer_product_details/index?id='+e.detail,
    })
  },
    // 下拉刷新
    onScrollRefresh: function () {
      this.setData({
        'page.pageNum': 1,
        products:[]
      })
      this.getList()  
    },
      // 触底刷新
  getNext() {
    console.log('触底')
    if (this.data.page.pageNum * this.data.page.pageSize >= this.data.total) {
      return
    } else {
      this.data.page.pageNum++;
      this.getList();
    }
  },
  initType(){
    let component = this.selectComponent('#productType')
    if (component) {
      component.initInfo(1);
    }
  },
  productTypeConfirm(event){
    console.log('event',event)
    wx.setStorageSync('categoryType', event.detail);
    wx.switchTab({
      url: '/pages/customer_merchant_details/index',
    })
  }


})